// 引入路由
import VueRouter from 'vue-router'
import Vue from 'vue'

import auth from '../views/utils/auth.js'
// 引入组件
// 进行路由懒加载，直接写到路由规则中
// import Login from '../views/login/index.vue'
// import Layout from '../views/Layout/index.vue'
// import Home from '../views/home/index.vue'
// import Article from '../views/article/index.vue'
// import Publish from '../views/publish/index.vue'
// 注册
Vue.use(VueRouter)
// 规则配置
const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: () => import('../views/login/index.vue')
    },
    {
      path: '/',
      component: () => import('../views/Layout/index.vue'),
      children: [
        { path: '/', component: () => import('../views/home/index.vue') },
        { path: '/article', component: () => import('../views/article/index.vue') },
        { path: '/publish', component: () => import('../views/publish/index.vue') }
      ]
    }
  ] // 路由规则

})

router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})

export default router
